﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串不能匹配给定的正则表达式, 示例:
/// <code>
/// [VNotMatchRegex("132[0-9]{4}1234", "不能输入保留的手机号")]
/// public string PhoneNumber { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VNotMatchRegexAttribute(string pattern, string errorMessage = null) : Attribute, IValidateAction
{
    public string Pattern { get; set; } = pattern;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustNotMatchRegexInternal(ctx, ctx.ModelValue?.ToString(), Pattern, ErrorMessage);
    }
}
